/*
 * Decompiled with CFR 0.150.
 * 
 * Could not load the following classes:
 *  cn.hutool.core.util.ObjectUtil
 *  javafx.beans.property.ObjectProperty
 *  javafx.beans.property.SimpleObjectProperty
 *  javafx.beans.value.ObservableValue
 *  javafx.event.ActionEvent
 *  javafx.fxml.FXML
 *  javafx.fxml.FXMLLoader
 *  javafx.fxml.Initializable
 *  javafx.scene.Node
 *  javafx.scene.control.Button
 *  javafx.scene.control.TextField
 *  javafx.scene.layout.HBox
 *  javafx.stage.Popup
 *  javafx.stage.Window
 */
package com.gys.common.plugIn.dateTimePicker;

import cn.hutool.core.util.ObjectUtil;
import com.gys.common.plugIn.dateTimePicker.DateTimePickerPopup;
import com.gys.common.plugIn.dateTimePicker.HoursPicker;
import com.gys.common.util.CommonUtil;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.URL;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.time.temporal.TemporalAccessor;
import java.util.ResourceBundle;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.stage.Popup;
import javafx.stage.Window;

public class DateTimePicker
extends HBox
implements Initializable {
    private ObjectProperty<LocalDateTime> dateTime;
    private final DateTimeFormatter formatter;
    private final Popup popupContainer;
    private final DateTimePickerPopup popup;
    @FXML
    private TextField textField;
    @FXML
    private Button button;
    private Object data;
    private String property;
    private Class classParam;

    public DateTimePicker() {
        this(LocalDateTime.now());
    }

    public DateTimePicker(LocalDateTime dateTime) {
        this(dateTime, DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT));
    }

    public DateTimePicker(Object data, String property, Class classParam) {
        this(LocalDateTime.now());
        this.data = data;
        this.property = property;
        this.classParam = classParam;
    }

    public DateTimePicker(LocalDateTime dateTime, DateTimeFormatter formatter) {
        this.dateTime = new SimpleObjectProperty(dateTime);
        this.formatter = formatter;
        this.popupContainer = new Popup();
        this.popup = new DateTimePickerPopup(this);
        URL url = HoursPicker.class.getClass().getResource("/fxml/dateTimePicker/DateTimePicker.fxml");
        FXMLLoader fxmlLoader = new FXMLLoader();
        try {
            fxmlLoader = new FXMLLoader(url);
        }
        catch (Exception e) {
            throw new RuntimeException(e);
        }
        fxmlLoader.setRoot(this);
        fxmlLoader.setController(this);
        try {
            fxmlLoader.load();
        }
        catch (IOException ex) {
            throw new RuntimeException(ex);
        }
    }

    public void setTime(String value) {
        this.textField.setText(value);
    }

    public String getTime() {
        return this.textField.getText();
    }

    public void initialize(URL location, ResourceBundle resources) {
        this.dateTime.addListener((observable, oldValue, newValue) -> {
            this.popup.setDate(newValue.toLocalDate());
            this.popup.setTime(newValue.toLocalTime());
            this.textField.setText(this.formatter.format((TemporalAccessor)newValue));
        });
        this.button.prefHeightProperty().bind((ObservableValue)this.textField.heightProperty());
        this.popupContainer.getContent().add(this.popup);
        this.popupContainer.autoHideProperty().set(true);
    }

    public ObjectProperty<LocalDateTime> dateTimeProperty() {
        return this.dateTime;
    }

    void hidePopup() {
        LocalTime time = this.popup.getTime();
        String timeStr = time.toString().substring(0, 5);
        this.textField.setText(timeStr);
        if (ObjectUtil.isNotEmpty(this.data) && ObjectUtil.isNotEmpty(this.property)) {
            try {
                Method method = this.data.getClass().getMethod(CommonUtil.getSetMethod(this.property), this.classParam);
                method.invoke(this.data, timeStr);
            }
            catch (Exception ex) {
                throw new RuntimeException(this.property + "\u6ca1\u6709set\u65b9\u6cd5");
            }
        }
        this.popupContainer.hide();
    }

    @FXML
    void handleButtonAction(ActionEvent event) {
        if (this.popupContainer.isShowing()) {
            this.popupContainer.hide();
        } else {
            Window window = this.button.getScene().getWindow();
            double x = window.getX() + this.textField.localToScene(0.0, 0.0).getX() + this.textField.getScene().getX();
            double y = window.getY() + this.button.localToScene(0.0, 0.0).getY() + this.button.getScene().getY() + this.button.getHeight();
            this.popupContainer.show((Node)this.getParent(), x, y);
        }
    }
}

